<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-3-6
 * Time: 下午5:33
 */

namespace common\service;


class TreeService extends BaseService
{
    /**
     * 获取树形
     * @param $arr
     * @param int $pid
     * @param int $level
     * @return array
     */
    public static function getTrees($arr,$pid=0,$level=0){
        $list = [];
        foreach($arr as $key=>$value)
        {
            if($value['pid'] == $pid)
            {
//                $value['level'] = $level;
                array_push($list,$value);
            }
        }
        if(empty($list)) {
            return $list;
        }else{
            foreach($list as $key=>$value)
            {
                $list[$key]['child'] = self::getTrees($arr,$value['id'],$level + 1);
            }
        }
        return $list;//递归出口
    }

    public static function getTreeCategory($arr,$pid=0,$level=0){
        $list = [];
        foreach($arr as $key=>$value)
        {
            if($value['pid'] == $pid)
            {
//                $value['level'] = $level;
                array_push($list,$value);
            }
        }
        if(empty($list)) {
            return $list;
        }else{
            $data = [];
            foreach($list as $key=>$value)
            {
                array_push($data,$value);
                $data = array_merge($data,self::getTreeCategory($arr,$value['id'],$level + 1));
            }
        }
        return $data;//递归出口
    }

}